package com.ffcs.cn.action.labelBase.workspace;

import com.supermap.data.Datasource;
import com.supermap.data.DatasourceConnectionInfo;
import com.supermap.data.EngineType;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.utilities.ComponentFactory;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.ffcs.cn.DevelopProperties;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;

/**
 * 打开数据库型数据源对话框
 *
  */
public class DialogOpenDatabase extends SmDialog {
	/**
	 * 数据库引擎类型
	 */
	private JLabel jLabelEngineType;
	private JComboBox<EngineType> jComboBoxEngineType;

	/**
	 * 数据库服务器名称
	 */
	private JLabel jLabelServer;
	private JTextField jTextFieldServer;

	/**
	 * 数据库名
	 */
	private JLabel jLabelDatabaseName;
	private JTextField jTextFieldDatabaseName;

	/**
	 * 用户名
	 */
	private JLabel jLabelUser;
	private JTextField jTextFieldUser;

	/**
	 * 密码
	 */
	private JLabel jLabelPassword;
	private JPasswordField jTextFieldPassword;

	/**
	 * 别名
	 */
	private JLabel jLabelAlias;
	private JTextField textFieldAlias;

	private JButton buttonOk;
	private JButton buttonCancel;

	/**
	 * 数据库引擎类型，以MySQL、PostGIS、MongoDB为例
	 */
	private final EngineType[] engineTypes = {EngineType.MYSQL, EngineType.PGGIS, EngineType.MONGODB};

	public DialogOpenDatabase() {
		initComponents();
		initLayout();
		registerEvents();
	}

	private void initComponents() {
		setTitle(DevelopProperties.getString("String_Title_DatasourceOpenDatabase"));

		jLabelEngineType = new JLabel(DevelopProperties.getString("String_Label_DatabaseEngine"));
		jComboBoxEngineType = new JComboBox<>(engineTypes);

		jLabelServer = new JLabel(DevelopProperties.getString("String_Label_DatabaseServer"));
		jTextFieldServer = new JTextField();

		jLabelDatabaseName = new JLabel(DevelopProperties.getString("String_Label_DatabaseName"));
		jTextFieldDatabaseName = new JTextField();

		jLabelUser = new JLabel(DevelopProperties.getString("String_Label_DatabaseUser"));
		jTextFieldUser = new JTextField();

		jLabelPassword = new JLabel(DevelopProperties.getString("String_Label_DatabasePassword"));
		jTextFieldPassword = new JPasswordField();

		jLabelAlias = new JLabel(DevelopProperties.getString("String_Label_Alias"));
		textFieldAlias = new JTextField();

		if (jComboBoxEngineType.getSelectedItem() != null) {
			textFieldAlias.setText(jComboBoxEngineType.getSelectedItem().toString());
		}

		buttonOk = new JButton(DevelopProperties.getString("String_Button_OK"));
		buttonCancel = new JButton(DevelopProperties.getString("String_Button_Cancel"));
	}

	private void initLayout() {
		this.setSize(this.getPreferredSize());
		this.setLayout(new GridBagLayout());
		this.add(jLabelEngineType, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.NONE)
				.setWeight(0, 0));
		this.add(jComboBoxEngineType, new GridBagConstraintsHelper(1, 0, 2, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(1, 0));

		this.add(jLabelServer, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.NONE)
				.setWeight(0, 0));
		this.add(jTextFieldServer, new GridBagConstraintsHelper(1, 1, 2, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, 0, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(1, 0));

		this.add(jLabelDatabaseName, new GridBagConstraintsHelper(0, 2, 1, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.NONE)
				.setWeight(0, 0));
		this.add(jTextFieldDatabaseName, new GridBagConstraintsHelper(1, 2, 2, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, 0, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(1, 0));

		this.add(jLabelUser, new GridBagConstraintsHelper(0, 3, 1, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.NONE)
				.setWeight(0, 0));
		this.add(jTextFieldUser, new GridBagConstraintsHelper(1, 3, 2, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, 0, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(1, 0));

		this.add(jLabelPassword, new GridBagConstraintsHelper(0, 4, 1, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.NONE)
				.setWeight(0, 0));
		this.add(jTextFieldPassword, new GridBagConstraintsHelper(1, 4, 2, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, 0, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(1, 0));

		this.add(jLabelAlias, new GridBagConstraintsHelper(0, 5, 2, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(0, 0));

		this.add(textFieldAlias, new GridBagConstraintsHelper(1, 5, 2, 1)
				.setAnchor(GridBagConstraints.WEST)
				.setInsets(0, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(1, 0));

		this.add(new JPanel(), new GridBagConstraintsHelper(0, 6, 3, 1)
				.setAnchor(GridBagConstraints.CENTER)
				.setFill(GridBagConstraints.BOTH)
				.setWeight(1, 1));
		this.add(ComponentFactory.createButtonPanel(buttonOk, buttonCancel), new GridBagConstraintsHelper(0, 7, 3, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.NONE)
				.setAnchor(GridBagConstraints.EAST));

	}

	public void registerEvents() {
		jComboBoxEngineType.addItemListener(e -> {
			if (e.getStateChange() == ItemEvent.SELECTED) {
				textFieldAlias.setText(e.getItem().toString());
			}
		});
		buttonOk.addActionListener(e -> {
			DatasourceConnectionInfo info = new DatasourceConnectionInfo();
			info.setEngineType((EngineType) jComboBoxEngineType.getSelectedItem());
			info.setServer(jTextFieldServer.getText());
			info.setDatabase(jTextFieldDatabaseName.getText());
			info.setUser(jTextFieldUser.getText());
			info.setPassword(String.valueOf(jTextFieldPassword.getPassword()));
			try {
				Datasource datasource = Application.getActiveApplication().getWorkspace().getDatasources().open(info);
				if (datasource == null) {
					Application.getActiveApplication().getOutput().output(DevelopProperties.getString("String_OpenDatabaseFail"));
				} else {
					Application.getActiveApplication().getOutput().output(DevelopProperties.getString("String_OpenDatabaseSucceed"));
					this.dispose();
				}
			} catch (Exception exception) {
				Application.getActiveApplication().getOutput().output(exception.getMessage());
			}
		});
		buttonCancel.addActionListener(e -> this.dispose());
	}
}
